﻿@page "/configdata/index"
@attribute [Authorize]
@inject IDataConfigService DataConfigService
@inject IConfigDataService ConfigDataService
@inject IDialogService DialogService
@inject ExampleJsInterop JsInterop

<MudStack>
    @*<MudPaper Class="list-filter">
    <MudForm Model="_searchRequestDto">
    <MudGrid>
    <MudItem xs="12" md="6" lg="3">
    <MudTextField Label="用户名" @bind-Value="@_searchRequestDto.UserName" For="@(() => _searchRequestDto.UserName)"></MudTextField>
    </MudItem>
    <MudItem xs="12" md="6" lg="3">
    <MudTextField Label="姓名" @bind-Value="@_searchRequestDto.RealName" For="@(() => _searchRequestDto.RealName)"></MudTextField>
    </MudItem>
    <MudItem xs="12" md="6" lg="3">
    <MudTextField Label="联系方式" @bind-Value="@_searchRequestDto.Phone" For="@(() => _searchRequestDto.Phone)"></MudTextField>
    </MudItem>
    <MudItem xs="12" md="6" lg="3">
    <MudTextField Label="邮箱" @bind-Value="@_searchRequestDto.Email" For="@(() => _searchRequestDto.Email)"></MudTextField>
    </MudItem>
    <MudItem xs="12" md="6" lg="3">
    <MudSelect Label="数据状态" @bind-Value="@_searchRequestDto.Status" For="@(() => _searchRequestDto.Status)" Dense="true">
    <MudSelectItem Value="1">Enabled</MudSelectItem>
    <MudSelectItem Value="2">Disabled</MudSelectItem>
    </MudSelect>
    </MudItem>
    <MudItem xs="12" md="6" lg="3" Class="button-item">
    <MudButton Variant="Variant.Filled" StartIcon="@Icons.Material.Filled.Search" Color="Color.Primary" Size="Size.Small">查询</MudButton>
    </MudItem>
    </MudGrid>
    </MudForm>
    </MudPaper>*@
    <MudPaper Class="list-table">
        <MudTable @ref="_requestTable" ServerData="@(new Func<TableState, Task<TableData<ConfigDataInfoResponseDto>>>(ServerReload))"
                  MultiSelection="true" Hover="true" Dense="true" Breakpoint="Breakpoint.Xs" Elevation="0"
                  ReadOnly="true" FixedHeader="true" CustomFooter="true">
            <ToolBarContent>
                <MudButton StartIcon="@Icons.Material.Filled.Add" Variant="Variant.Filled" Color="Color.Primary"
                           Size="Size.Small" OnClick="AddHandler">
                    新增
                </MudButton>
                <MudButton StartIcon="@Icons.Material.Filled.Remove" Variant="Variant.Filled" Color="Color.Error"
                           Size="Size.Small" Class="ml-3">
                    删除
                </MudButton>
                <MudButton StartIcon="@Icons.Material.Filled.FileUpload" Variant="Variant.Filled" Color="Color.Primary"
                           Size="Size.Small" Class="ml-3">
                    导入
                </MudButton>
                <MudMenu StartIcon="@Icons.Material.Filled.FileDownload" Variant="Variant.Filled" Color="Color.Primary"
                         Size="Size.Small" Label="导出" Dense="true"
                         AnchorOrigin="Origin.BottomLeft" TransformOrigin="Origin.TopLeft"
                         Class="ml-3">
                    <MudMenuItem IconSize="Size.Small" IconColor="Color.Primary" Icon="@Icons.Custom.FileFormats.FileExcel">
                        导出Excel
                    </MudMenuItem>
                    <MudMenuItem IconSize="Size.Small" IconColor="Color.Primary" Icon="@Icons.Material.Filled.Chair">
                        下载模板
                    </MudMenuItem>
                    <MudMenuItem IconSize="Size.Small" IconColor="Color.Primary" Icon="@Icons.Material.Filled.Chair">导出历史</MudMenuItem>
                </MudMenu>
            </ToolBarContent>
            <Columns>
                @CreateColumns(context)

                <MudTemplateColumn T="ConfigDataInfoResponseDto">
                    <Header Context="item">
                        <MudTh>操作</MudTh>
                    </Header>
                    <Row Context="item">
                        <MudTd>
                            <MudButton StartIcon="@Icons.Material.Filled.Edit" Size="Size.Small">修改</MudButton>
                        </MudTd>
                    </Row>
                </MudTemplateColumn>
            </Columns>
            <PagerContent>
                <MudTablePager/>
            </PagerContent>
        </MudTable>
    </MudPaper>
</MudStack>

@code {
    private ConfigDataSearchRequestDto _searchRequestDto = new();
    private IList<ConfigFieldInfoResponseDto> _allFields = new List<ConfigFieldInfoResponseDto>();
    private MudTable<ConfigDataInfoResponseDto> _requestTable = new();
    private IEnumerable<ConfigDataInfoResponseDto> _pagedData = new List<ConfigDataInfoResponseDto>();
    private int _totalItems = 0;

    private DialogOptions _dialogOptions = new()
    {
        CloseButton = true,
        FullWidth = true,
        DisableBackdropClick = true
    };

    [Parameter]
    [SupplyParameterFromQuery(Name = "ConfigCode")]
    [NotNull]
    public string ConfigCode { get; set; } = string.Empty;

    protected override async Task OnInitializedAsync()
    {
        var result = await DataConfigService.QueryDataConfigFields(ConfigCode,
            new ConfigFieldSearchRequestDto());
        if (result != null && result.Data != null)
        {
            _allFields = result.Data;
        }

        await base.OnInitializedAsync();
    }

    async Task AddHandler()
    {
        var parameters = new DialogParameters { { "ConfigCode", ConfigCode } };
        _dialogOptions.MaxWidth = MaxWidth.Large;
        var dialog = await DialogService.ShowAsync<ConfigDataEdit>("新增数据", parameters, _dialogOptions);
        var result = await dialog.Result;
        if (!result.Canceled)
        {
            await _requestTable.ReloadServerData();
        }
    }

    private async Task<TableData<ConfigDataInfoResponseDto>> ServerReload(TableState state)
    {
        _searchRequestDto.Page = state.Page + 1;
        _searchRequestDto.Limit = state.PageSize;
        var data = await ConfigDataService.QueryConfigDatasByPage(_searchRequestDto);

        if (data != null)
        {
            _pagedData = data.Data;
            _totalItems = data.TotalCount;
        }

        return new TableData<ConfigDataInfoResponseDto>() { TotalItems = _totalItems, Items = _pagedData };
    }

    private RenderFragment CreateColumns(ConfigDataInfoResponseDto context)
    {
        Type type = typeof(ConfigDataInfoResponseDto);

        return
            @<Template>
                @foreach (var field in _allFields)
                {
                    if (field.IsVisible)
                    {
                        var propertyInfo = type.GetProperty(field.ValueColumnMap);
                        <MudColumn HeaderText="@field.FieldName" Value="propertyInfo?.GetValue(context)"></MudColumn>
                    }
                }
            </Template>;
    }

}